package com.justech.eat.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.justech.common.vo.PageDto;
import com.justech.eat.controller.req.OrderDayCount;
import com.justech.eat.controller.req.OrderDetailReq;
import com.justech.eat.entity.Order;
import com.justech.eat.entity.OrderDetail;
import com.justech.eat.entity.OrderDetailAbnormalRecord;
import com.justech.eat.mapper.mysql.OrderDetailAbnormalRecordMapper;
import com.justech.eat.mapper.mysql.OrderDetailMapper;
import com.justech.eat.mapper.mysql.OrderMapper;
import com.justech.eat.service.OrderBook;
import com.justech.eat.service.OrderMgrService;
import com.justech.eat.util.PageBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderMgrServiceImp implements OrderMgrService {


    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderBook orderBook;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    OrderDetailAbnormalRecordMapper orderDetailAbnormalRecordMapper;


    /**
     * 批量添加报餐信息
     *
     * @param orderDetailReqs
     */
    @Override
    public void submitOrders(List<OrderDetailReq> orderDetailReqs, String date, String jobNumber) {
        for (int i = 0; i < orderDetailReqs.size(); i++) {
            orderBook.submit(orderDetailReqs.get(i), date, jobNumber);
        }

    }

    /**
     * 提交报餐
     *
     * @param orderDetailReqs
     */
    @Override
    public void submitOrder(OrderDetailReq orderDetailReqs, String date, String jobNumber) {
        orderBook.submit(orderDetailReqs, date, jobNumber);
    }

    /**
     * 批量处理异常
     *
     * @param abnormalIds 工号列表
     * @param reason      异常原因 (接收随意输入的字符)
     * @param reasonCode  异常原因代码 (如果不知道具体的{@reasonCode}可以传入0)
     */
    @Override
    public void explainOrderAbnormals(List<Long> abnormalIds, String reason, int reasonCode) {
        for (int i = 0; i < abnormalIds.size(); i++) {
            //通过异常id找到对应异常信息
            OrderDetailAbnormalRecord orderDetailAbnormalRecord = orderDetailAbnormalRecordMapper.queryById(abnormalIds.get(i));
            if (orderDetailAbnormalRecord != null) {
                OrderDetail orderDetail = orderDetailMapper.queryById(orderDetailAbnormalRecord.getOrderDetailId());
                if (orderDetail != null) {
                    //改变orderDetail表里的状态result、updateTime和UpdateBy
                    if (orderDetail.getStatus() == 1) {
                        orderDetail.setResult(0);
                        orderDetail.setUpdateTime(new Date());
                        orderDetail.setUpdateBy("季超");
                        orderDetailMapper.update(orderDetail);
                    }
                    //更改orderDetailAbnormalRecord状态为1、增加reason和reasonCode
                    orderDetailAbnormalRecord.setStatus(1);
                    orderDetailAbnormalRecord.setReason(reason);
                    orderDetailAbnormalRecord.setReasonCode(reasonCode);
                    orderDetailAbnormalRecordMapper.update(orderDetailAbnormalRecord);
                }
            }
        }
    }


    /**
     * 处理异常
     *
     * <strong>
     * 处理异常前需注意一下：
     * 1、检查当前用户是否存在异常
     * 2、检查日期是否合法(必须是已经过去的日期)
     * </strong>
     *
     * @param reason
     * @param reasonCode
     */
    @Override
    public void explainOrderAbnormal(Long abnormalId, String reason, int reasonCode) {
        //通过异常id找到对应异常信息
        OrderDetailAbnormalRecord orderDetailAbnormalRecord = orderDetailAbnormalRecordMapper.queryById(abnormalId);
        //通过orderDetailAbnormalRecord表的OrderDetailId判断OrderDetail表里是否存在
        if (orderDetailAbnormalRecord != null) {
            OrderDetail orderDetail = orderDetailMapper.queryById(orderDetailAbnormalRecord.getOrderDetailId());
            if (orderDetail != null) {
                //改变orderDetail表里的状态result、updateTime和UpdateBy
                if (orderDetail.getStatus() == 1) {
                    orderDetail.setResult(0);
                    orderDetail.setUpdateTime(new Date());
                    orderDetail.setUpdateBy("季超");
                    orderDetailMapper.update(orderDetail);
                }
                //更改orderDetailAbnormalRecord状态为1、增加reason和reasonCode
                orderDetailAbnormalRecord.setStatus(1);
                orderDetailAbnormalRecord.setReason(reason);
                orderDetailAbnormalRecord.setReasonCode(reasonCode);
                orderDetailAbnormalRecordMapper.update(orderDetailAbnormalRecord);
            }
        }
    }

    /**
     * 异常列表
     *
     * @param params
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDto<OrderDetailAbnormalRecord> getAbnormalList(OrderDetailAbnormalRecord params, long page, int size) {
        PageHelper.startPage((int) page, size);
        List<OrderDetailAbnormalRecord> list = orderDetailAbnormalRecordMapper.findAll(params);
        PageInfo<OrderDetailAbnormalRecord> pageInfo = new PageInfo<>(list);
        return PageBuilder.build(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getSize(), pageInfo.getTotal());
    }

    /**
     * 报餐明细
     *
     * @param order
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDto<Order> getOrderList(Order order, long page, int size) {
        PageHelper.startPage((int) page, size);
        List<Order> list = orderMapper.findAll(order);
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        return PageBuilder.build(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getSize(), pageInfo.getTotal());
    }

    /**
     * 报餐详情(包含每餐明细)
     *
     * @param order
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDto<Order> getOrderDetailList(Order order, long page, int size) {
        PageHelper.startPage((int) page, size);
        List<Order> all = orderMapper.findAll(order);
        PageInfo<Order> pageInfo = new PageInfo<>(all);
        PageDto<Order> build = PageBuilder.build(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getSize(), pageInfo.getTotal());
        List<Order> orders = build.getList();
        for (Order order1 : orders) {
            List<OrderDetail> orderDetails = orderDetailMapper.findByOrderId(order1.getId());
            order1.setOrderDetails(orderDetails);
        }
        return build;
    }

    /**
     * 查询当天各餐别的报餐数量
     */
    @Override
    public OrderDayCount findAllDetail() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sj = sdf.format(d);
        List<Map<Integer, Integer>> list = orderDetailMapper.findAllDetail(sj);
        Map<Integer, Integer> parseMap = parseMap(list);
        OrderDayCount dayCount = new OrderDayCount();
        dayCount.setLunch(parseMap.get(1) == null ? 0 : Integer.valueOf(String.valueOf(parseMap.get(1))));
        dayCount.setDinner(parseMap.get(2) == null ? 0 : Integer.valueOf(String.valueOf(parseMap.get(2))));
        dayCount.setMeal(parseMap.get(3) == null ? 0 : Integer.valueOf(String.valueOf(parseMap.get(3))));
        dayCount.setNight(parseMap.get(4) == null ? 0 : Integer.valueOf(String.valueOf(parseMap.get(4))));
        return dayCount;
    }

    private Map<Integer, Integer> parseMap(List<Map<Integer, Integer>> list) {
        HashMap<Integer, Integer> integerHashMap = new HashMap<>(4);
        for (Map<Integer, Integer> integerIntegerMap : list) {
            integerHashMap.put(integerIntegerMap.get("type"), integerIntegerMap.get("count"));
        }
        return integerHashMap;
    }


}
